<?php

namespace app\api\controller;

use app\admin\model\Goods;
use app\common\controller\Api;
use app\common\model\Config;
use think\Db;
use think\Model;
use think\process\Utils;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {

        $str = "9*9*6*6 5*6*8 + 5*69*8*6 9*9*9";
        $str = "";
        $ar = explode("+",$str);
        p($ar);
        $arr = [];
        foreach ($ar as $ar_val){
            $br = explode(" ",$ar_val);
            p($br);
            $arr = array_merge($arr,$br);
        }
        p($arr);die;

        $this->success('请求成功');
    }

    public function search()
    {
        $code = $this->request->post("code");
        if(empty($code)){
            $code = $this->request->get("code");
        }
        $username = $this->request->post("username");
        if(empty($username)){
            $username = $this->request->get("username");
        }
        $day = $this->request->post("day");
        if(empty($day)){
            $day = $this->request->get("day");
        }
        $start_date = $this->request->post("start_date");
        if(empty($start_date)){
            $start_date = $this->request->get("start_date");
        }
        $end_date = $this->request->post("end_date");
        if(empty($end_date)){
            $end_date = $this->request->get("end_date");
        }
        $stock = (int)$this->request->post("stock",-1); //库存，1有库存，0无库存
        if(empty($stock)){
            $stock = $this->request->get("stock");
        }
        $page = (int)$this->request->post('page',1);
        if(empty($page)){
            $page = $this->request->get("page");
        }

        $model = new \app\admin\model\Goods;

        if($code){
            $model->where("goods_sn",$code);
        }
        if($username){
           $model->where("username",$username);
        }
        if($day == 'today'){
            $date = date("Y-m-d");
            $start_time = strtotime($date);
            $end_time = $start_time + 86400;
            $model->where("createtime",">=",$start_time);
            $model->where("createtime","<=",$end_time);
        }
        if($day == 'yesterday'){
            $date = date("Y-m-d");
            $end_time = strtotime($date);
            $start_time = $end_time - 86400;
            $model->where("createtime",">=",$start_time)->where("createtime","<",$end_time);
        }
        if($stock == 1){
            $model->where("`stock` > `chu_stock`");
        }
        if($stock == 0){
            $model->where("`stock` = `chu_stock`");
        }
        if($start_date){
            $time = strtotime($start_date);
            $where["createtime"]=[">=",$time];
        }
        if($end_date){
            $time = strtotime($end_date);
            $time = $time + 86400;
            $where["createtime"]=["<",$time];
        }

        $ar = $model->paginate();
        $ar = $ar->toArray();
        $sql = $model->getLastSql();
        $goods_list=$ar['data'];
        $totalRow = $ar['total'];
        $size = $ar['per_page'];
        if(empty($goods_list)){
            $this->error('数据不存在');
        }
        $goods_id_ids = array_column($goods_list,'id');
        $list_model = new \app\admin\model\CheckoutGoods;
        $list = $list_model->where('good_id',"in",$goods_id_ids)->select();
        $lists = [];

        foreach ($list as &$list_val){
            $list_val['date'] = date("Y-m-d H:i",$list_val['createtime']);
            $lists[$list_val['good_id']][] = $list_val;
        }
        foreach ($goods_list as &$goodsInfo){
            $goodsInfo['image'] = cdnurl( $goodsInfo['image'],true);
            $goodsInfo['date'] = date("Y-m-d H:i",$goodsInfo['createtime']);
            $goodsInfo['chuku_list'] = isset($lists[$goodsInfo['id']])?$lists[$goodsInfo['id']]:"";
        }
        $data_list = [];
        foreach ($goods_list as $val){
            $da = [
                'username'=>$val['username'],
                'date'=>$val['date'],
                'chuku_date'=>'',
                'goods_sn'=>$val['goods_sn'],
                'stock'=>$val['stock'],
                'chuku_stock'=>"",
                'weight'=>$val['weight'],
                'volume_str'=>$val['volume_str'],
                'volume'=>$val['volume'],
                'pack'=>$val['pack'],
                'goods_state'=>$val['goods_state'],
                'wenjian_state'=>$val['wenjian_state'],
                'ku_number'=>$val['ku_number'],
                'price'=>$val['price'],
                'note'=>$val['note'],
                'image'=>$val['image'],
                'createtime'=>$val['createtime'],
                'createtime_str'=>date("Y-m-d H:i:s",$val['createtime']),
                'chuku_createtime'=>"",
                'chuku_createtime_str'=>"",
            ];
            if($val['chuku_list'] && is_array($val['chuku_list'])){
                foreach ($val['chuku_list'] as $val1){
                    $da['chuku_date'] = $val1['date'];
                    $da['chuku_stock'] = $val1['stock'];
                    $da['chuku_createtime'] = $val1['createtime'];
                    $da['chuku_createtime_str'] = date("Y-m-d H:i:s",$val1['createtime']);
                    $data_list[] = $da;
                }
            }else{
                $data_list[] = $da;
            }
        }
        $max_page = ceil($totalRow/$size);
        $logo =\config("site.logo");
        $logo = cdnurl($logo,true);

        $data = [
            "list"=>$data_list,
            "page"=>$page,
            "max_page"=>$max_page,
            'sql'=>$sql,
            'logo'=>$logo
        ];
        $this->success('ok',$data);
    }
}
